﻿#ifndef DEVICE_H
#define DEVICE_H

#include <QObject>
#include <mmdeviceapi.h>
#include <Audioclient.h>
#include <Endpointvolume.h>
#include <Functiondiscoverykeys_devpkey.h>

extern "C"
{
#include "libavcodec/avcodec.h"
#include "libswresample/swresample.h"
}

#define SAFE_RELEASE(punk)  \
              if ((punk) != NULL)  \
                { (punk)->Release(); (punk) = NULL; }

class device : public QObject
{
    Q_OBJECT
public:
    explicit device(QObject *parent = nullptr);
    ~device();
    bool open(int index);
    bool close();
    QStringList list() const;
    void getBuffer(char** buffer, int &size, uint *nextPacketSize);

signals:

private:
    void init();
    IMMDeviceEnumerator *m_enumerator = nullptr;
    IMMDeviceCollection *m_collection = nullptr;
    IMMDevice *m_device = nullptr;
    IAudioClient* m_audio_client = nullptr;
    IAudioCaptureClient* m_audio_client_capture = nullptr;
    WAVEFORMATEX* m_waveformat = nullptr;

    QStringList m_device_list;

    //音频格式转换
    SwrContext* m_swr = nullptr;
    void initSwr();
    uchar* m_out_audio_data = nullptr;
};

#endif // DEVICE_H
